"""
注册定时任务
"""

from datetime import timezone
from  .models import TaskRegistry,  TaskExecutionLog
from ctbloge.apps.accounts.models import PasswordResetCode
import logging

@TaskRegistry.register("发送邮件提醒")
def send_reminder():
    """示例：发送提醒任务"""
    from django.core.mail import send_mail
    send_mail(
        '定时提醒',
        '这是来自系统的自动提醒',
        '18748500526@163.com',
        ['18748500526@163.com'],
        fail_silently=False,
    )
    return logging.info("发送邮件提醒成功")

@TaskRegistry.register("生成报告")
def delete_old_codes():
    #定时清理验证码
    PasswordResetCode.objects.filter(created_date__lt=timezone.now()-timezone.timedelta(days=7)).delete()
    return logging.info("验证码清理成功")

@TaskRegistry.register('清理定时任务日志')
def delete_old_logs():
    #清理定时任务日志
    TaskExecutionLog.objects.filter(create_time__lt=timezone.now()-timezone.timedelta(days=7)).delete()
    return logging.info("定时任务日志清理成功")


@TaskRegistry.register("导入Markdown文章")
def import_articles_from_md():
    from django.core.management import call_command
    call_command('import_articles_from_md')
    return logging.info("导入Markdown文章成功")


#备份数据库
@TaskRegistry.register("备份数据库")
def backup_database():
    from django.core.management import call_command
    call_command('backup_database')
    return logging.info("数据库备份成功")

#检查友情链接
@TaskRegistry.register("检查友情链接")
def check_links():
    from django.core.management import call_command
    call_command('check_links')
    return logging.info("检查友情链接成功")